home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-10-24 | 7.1 KB | 244 lines | [TEXT/KAHL] |
- ///--------------------------------------------------------------------------------------
- // SWGameUtils.c
- //
- // Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide.
- //
- // Description: some utility functions for games
- ///--------------------------------------------------------------------------------------
-
-
- #include <QDOffscreen.h>
- #include <SWCommonHeaders.h>
- #include <SWGameUtils.h>
-
-
- ///--------------------------------------------------------------------------------------
- // Randomize - initialize random number seed
- ///--------------------------------------------------------------------------------------
-
- pascal void Randomize( void )
- {
- GetDateTime( (unsigned long *)(&qd.randSeed) );
- }
-
-
- ///--------------------------------------------------------------------------------------
- // GetRandom - generate a random number between min and max inclusive
- ///--------------------------------------------------------------------------------------
-
- pascal short GetRandom(short min, short max)
- {
- unsigned short random;
- long range, temp;
-
- random = Random();
- range = (max - min) + 1;
- temp = (random * range) / 65536;
- random = temp + min;
-
- return random;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CenterRect - centers rectA in rectB
- ///--------------------------------------------------------------------------------------
-
- pascal void CenterRect(Rect* rectA, Rect* rectB)
- {
- short width = (rectA->right - rectA->left);
- short height = (rectA->bottom - rectA->top);
-
- rectA->left = rectB->left + (((rectB->right - rectB->left) / 2) - (width / 2));
- rectA->top = rectB->top + (((rectB->bottom - rectB->top) / 2) - (height / 2));
- rectA->right = rectA->left + width;
- rectA->bottom = rectA->top + height;
- }
-
-
- short gOldEventMask; // Used by AllowKeyUpEvents and RestoreEventMask
- Boolean eventMaskIsGood = false;
-
- ///--------------------------------------------------------------------------------------
- // AllowKeyUpEvents - allows keyUpEvents. Make sure to call RestoreEventMask
- // before your program quits if you call AllowKeyUpEvents.
- ///--------------------------------------------------------------------------------------
-
- pascal void AllowKeyUpEvents( void )
- {
- gOldEventMask = LMGetSysEvtMask();
- SetEventMask(everyEvent);
-
- // Let RestoreEventMask know that the old mask has been saved
- eventMaskIsGood = true;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // RestoreEventMask - call this before your program quits if you previously
- // called AllowKeyUpEvents. This will beep if you never called AllowKeyUpEvents first.
- ///--------------------------------------------------------------------------------------
-
- pascal void RestoreEventMask( void )
- {
- if (eventMaskIsGood)
- SetEventMask(gOldEventMask);
- else
- SysBeep(1);
- }
-
-
-
- ///--------------------------------------------------------------------------------------
- // Globals for HideMenuBar and ShowMenuBar
- ///--------------------------------------------------------------------------------------
-
- RgnHandle gOldVisRgn = NULL; // visRgn of window before hiding menu bar
- RgnHandle gUpdateRgn = NULL; // region returned to user
- short gOldMBarHeight;
-
-
- ///--------------------------------------------------------------------------------------
- // HideMenuBar - expands the vis region of grafPort to cover the entire window, which
- // will allow you to draw in the top of that window to erase the menu bar. This is a
- // simple routine designed for programs with only one window that covers the menu bar.
- // If you need to expand the region of more than one window, you need a different routine.
- // Be sure to make the window visible before calling this. HideMenuBar returns the
- // region of the menu bar and corners of the screen, in case you want to erase or
- // draw in that area.
- ///--------------------------------------------------------------------------------------
-
- pascal RgnHandle HideMenuBar(GrafPtr grafPort)
- {
- RgnHandle newVisRgn;
- GrafPtr savePort;
-
- if (gOldVisRgn != NULL)
- return NULL;
-
- GetPort(&savePort);
- SetPort(grafPort);
-
- gOldMBarHeight = LMGetMBarHeight();
- LMSetMBarHeight( 0 ); // Keeps things like SuperClock from coming on.
-
- // save off vis region
- gOldVisRgn = NewRgn();
- CopyRgn(grafPort->visRgn, gOldVisRgn);
-
- // expand the vis region of the port rect to be completely rectangular
- newVisRgn = NewRgn();
- RectRgn(newVisRgn, &grafPort->portRect);
- CopyRgn(newVisRgn, grafPort->visRgn);
- DisposeRgn(newVisRgn);
-
- // set gUpdateRgn to region of rounder corners and menu bar
- gUpdateRgn = NewRgn();
- CopyRgn(gOldVisRgn, gUpdateRgn);
- DiffRgn(grafPort->visRgn, gUpdateRgn, gUpdateRgn);
-
- SetPort(savePort);
- return gUpdateRgn;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // ShowMenuBar - restores the grafPort to the way it was before the call to HideMenuBar.
- // Make sure to call this after every call to HideMenuBar to dispose of gOldVisRgn.
- ///--------------------------------------------------------------------------------------
-
- pascal void ShowMenuBar(GrafPtr grafPort)
- {
- GrafPtr savePort;
- RgnHandle junkRgn;
-
- if (gOldVisRgn == NULL)
- return;
-
- GetPort(&savePort);
- SetPort(grafPort);
-
- LMSetMBarHeight( gOldMBarHeight );
-
- // fill the rounded corners of the screen with black again
- junkRgn = NewRgn();
- CopyRgn(gOldVisRgn, junkRgn);
- DiffRgn(grafPort->visRgn, junkRgn, junkRgn);
-
- #ifdef dangerousPattern
- FillRgn(junkRgn, qd.black);
- #else
- FillRgn(junkRgn, &qd.black);
- #endif
-
- DisposeRgn(junkRgn);
-
- // restore the old vis region
- CopyRgn(gOldVisRgn, grafPort->visRgn);
- DisposeRgn(gOldVisRgn);
- gOldVisRgn = NULL;
-
- DisposeRgn(gUpdateRgn);
- gUpdateRgn = NULL;
-
- DrawMenuBar();
-
- SetPort(savePort);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // GetDepthFromWindow
- ///--------------------------------------------------------------------------------------
-
- pascal short GetDepthFromWindow( WindowPtr theWindow )
- {
- GWorldPtr oldGWorld, windowGWld;
- GDHandle oldGDH, windowGDH;
- short depth;
-
- GetGWorld( &oldGWorld, &oldGDH );
- SetPort( theWindow );
- GetGWorld( &windowGWld, &windowGDH );
-
- depth = GetScreenDepth( windowGDH );
-
- SetGWorld( oldGWorld, oldGDH );
- return depth;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // GetScreenDepth
- ///--------------------------------------------------------------------------------------
-
- pascal short GetScreenDepth( GDHandle theGDH )
- {
- SysEnvRec theSERec;
- PixMapHandle thePixMap;
-
-
- SysEnvirons( 2, &theSERec );
- if ( !theSERec.hasColorQD ) // no colorQD?
- return 1;
- else
- {
- thePixMap = (**theGDH).gdPMap;
- return (**thePixMap).pixelSize; // Depth in bits (1,2,4...)
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // HasSystem7
- ///--------------------------------------------------------------------------------------
-
- pascal Boolean HasSystem7( void )
- {
- long gestaltResponse;
- OSErr err;
-
- err = Gestalt( gestaltSystemVersion, &gestaltResponse );
- return ( err == noErr && (gestaltResponse >= 0x0700));
- }
-